package com.secneo.appshare.activities;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.secneo.appshare.R;
import com.secneo.appshare.api.LocalStore;
import com.secneo.appshare.api.impl.RemoteApiImpl;
import com.secneo.appshare.common.AppSharePreference;
import com.secneo.appshare.common.Constants;

public class ApplicationActivity extends ActivityGroup{
	private Button retBtn ;
	private String package_name;
	private int number;
	private String uid;
	private Boolean installed_not_addicted;
	private Boolean user_app_flag;
	private String app_icon_url;
	private String app_name;
	private byte[] imageBytes;
	private Boolean firstIn = false;
	private Boolean doNothing = false;

	public enum AppActivityIndex {
		APPINFO, APPCOMMENTS, APPUSERS
	}

	private AppActivityIndex mActivityIndex;
	private RadioGroup mRadioGroup ;
	private RadioButton mAppInfoBtn;
	private RadioButton mAppCommentsBtn;
	private RadioButton mAppUsersBtn;
	private LinearLayout mContainerLinear;

	public static LinearLayout mHotcountButton;
	public static TextView mHotcountTextView;

	Handler mHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			switch(msg.what){
			case Constants.MSG_ACTION_MSG_HOT:
				mHotcountButton.setBackgroundResource(R.drawable.hot_press);
				mHotcountButton.setEnabled(false);
				try {
					String countStr = mHotcountTextView.getText().toString() ;
					int count = Integer.valueOf(countStr) ;
					++count ;
					mHotcountTextView.setText(""+count) ;
				} catch (NumberFormatException e) {
					e.printStackTrace();
				}
				break ;
			}
		}
	} ;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.appshare_application);

		parseIntent();

		mRadioGroup = (RadioGroup) findViewById(R.id.radioGroup) ;
		mRadioGroup.setOnCheckedChangeListener(mRadioGroupListener) ;
		mAppInfoBtn = (RadioButton) findViewById(R.id.toggle_descript);
		mAppCommentsBtn = (RadioButton) findViewById(R.id.toggle_comments);
		mAppUsersBtn = (RadioButton) findViewById(R.id.toggle_users);

		mHotcountButton = (LinearLayout) findViewById(R.id.HotcountButton);
		mHotcountButton.setOnClickListener(mHotListener) ;
		mHotcountTextView = (TextView) findViewById(R.id.HotcountTextView);

		mContainerLinear = (LinearLayout) findViewById(R.id.mainLinear);
		firstIn = true;
		mAppInfoBtn.performClick();
		
		retBtn = (Button) findViewById(R.id.img_ret) ;
		retBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish() ;
			}
		}) ;

	}

	private void parseIntent() {
		Bundle bundle = getIntent().getExtras();
		if (bundle != null) {
			package_name = bundle.getString("package_name");
			number = bundle.getInt("n", 20);
			uid = bundle.getString("uid");
			installed_not_addicted = bundle.getBoolean(
					"installed_not_addicted", true);
			user_app_flag = bundle.getBoolean("user_app_flag");
			app_icon_url = bundle.getString("app_icon");
			imageBytes = bundle.getByteArray("app_icon_array");
			app_name = bundle.getString("app_name");
		}
	}

	private Intent constructIntent(Intent intent) {
		intent.putExtra("package_name", package_name);
		intent.putExtra("n", number);
		intent.putExtra("uid", uid);
		intent.putExtra("installed_not_addicted", installed_not_addicted);
		intent.putExtra("app_icon", app_icon_url);
		intent.putExtra("app_icon_array", imageBytes);
		intent.putExtra("app_name", app_name);
		return intent;
	}

	private void setRecyleFlag() {
		ApplicationInfoActivity.mRecyleFlag = false;
		ApplicationUsersActivity.mRecyleFlag = false;
		ApplicationCommentsActivity.mRecyleFlag = false;

		if (mActivityIndex == AppActivityIndex.APPINFO)
			ApplicationInfoActivity.mRecyleFlag = true;
		else if (mActivityIndex == AppActivityIndex.APPCOMMENTS)
			ApplicationCommentsActivity.mRecyleFlag = true;
		else if (mActivityIndex == AppActivityIndex.APPUSERS)
			ApplicationUsersActivity.mRecyleFlag = true;
	}
	
	private RadioGroup.OnCheckedChangeListener mRadioGroupListener = new RadioGroup.OnCheckedChangeListener() {
		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) 
		{
			if(doNothing)
			{
				doNothing = false;
				return;
			}
			if(!AppSharePreference.isAppShareUserLogined(ApplicationActivity.this) && !firstIn)
			{
				Intent intent=new Intent();
				intent.setClass(ApplicationActivity.this, LoginChooseMethodActivity.class);
				startActivity(intent);
				
				doNothing = true;
				mAppInfoBtn.setChecked(true);
			}else
			{
				firstIn = false;
				setRecyleFlag();
				if (checkedId == mAppInfoBtn.getId()){
					mActivityIndex = AppActivityIndex.APPINFO;
					mContainerLinear.removeAllViews();
					Intent intent = new Intent(ApplicationActivity.this,
							ApplicationInfoActivity.class);
	
					intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).addFlags(
							Intent.FLAG_ACTIVITY_SINGLE_TOP)
							.putExtra("FromGroup", true);
	
					constructIntent(intent);
	
					mContainerLinear.addView(ApplicationActivity.this
							.getLocalActivityManager().startActivity("APPINFO", intent)
							.getDecorView(), LayoutParams.FILL_PARENT,
							LayoutParams.FILL_PARENT);
				} else if(checkedId == mAppCommentsBtn.getId()){
					mActivityIndex = AppActivityIndex.APPCOMMENTS;
					mContainerLinear.removeAllViews();
	
					Intent intent = new Intent(ApplicationActivity.this,
							ApplicationCommentsActivity.class);
	
					intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).addFlags(
							Intent.FLAG_ACTIVITY_SINGLE_TOP)
							.putExtra("FromGroup", true);
	
					constructIntent(intent);
	
					mContainerLinear.addView(ApplicationActivity.this
							.getLocalActivityManager().startActivity("APPCOMMENTS",
									intent).getDecorView(), LayoutParams.FILL_PARENT,
							LayoutParams.FILL_PARENT);
				} else if(checkedId == mAppUsersBtn.getId()){
					mActivityIndex = AppActivityIndex.APPUSERS;
					mContainerLinear.removeAllViews();
	
					Intent intent = new Intent(ApplicationActivity.this,
							ApplicationUsersActivity.class);
	
					intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).addFlags(
							Intent.FLAG_ACTIVITY_SINGLE_TOP)
							.putExtra("FromGroup", true);
	
					constructIntent(intent);
	
					mContainerLinear.addView(ApplicationActivity.this
							.getLocalActivityManager()
							.startActivity("APPUSERS", intent).getDecorView(),
							LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
				}
			}
		}
	};

	OnClickListener mHotListener = new OnClickListener() {

		@Override
		public void onClick(View v) 
		{
			if(!AppSharePreference.isAppShareUserLogined(ApplicationActivity.this))
			{
				Intent intent=new Intent();
				intent.setClass(ApplicationActivity.this, LoginChooseMethodActivity.class);
				startActivity(intent);
			}else
			{
				Thread hotThread = new Thread() {
					public void run() {
						RemoteApiImpl rai = new RemoteApiImpl();
						String uid = LocalStore.getLocaleStoreInstance(
								ApplicationActivity.this).getUid();
						JSONObject obj = rai.getUserAction(
								ApplicationActivity.this, uid, "", package_name,
								"6");
						if (obj != null) {
							try {
								if ("ok".equals(obj.getString("status"))) {
									Message msg = new Message();
									msg.what = Constants.MSG_ACTION_MSG_HOT;
									mHandler.sendMessage(msg);
								}
							} catch (JSONException e) {
								e.printStackTrace();
							}
						}
					}
				};
				hotThread.start();
			}
		}
	};

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

}
